package com.example.automatic.abs_bank.base;

import android.accessibilityservice.AccessibilityService;
import android.accessibilityservice.GestureDescription;
import android.graphics.Color;
import android.graphics.Path;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;
import android.widget.Button;

import androidx.appcompat.app.AlertDialog;

import com.example.automatic.MainActivity;
import com.example.automatic.okhttp.OnWebSocketListener;
import com.example.automatic.okhttp.WebSocketManager;
import com.example.automatic.utils.LogUtils;

import java.util.List;


public class BaseAccessibilityService extends AccessibilityService implements OnWebSocketListener {

    public WebSocketManager socketManager;


    public TransferAccount mTransferAccount;

    @Override
    protected void onServiceConnected() {
        super.onServiceConnected();
        socketManager = WebSocketManager.getInstance();
        socketManager.setOnWebSocketListener(this);
        socketManager.connect();
//        showFloatingWindow();
    }

    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {

    }

    @Override
    public void onInterrupt() {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        socketManager.close();
    }

    @Override
    public void onConnectSuccess() {
        LogUtils.e("WebSocket 连接成功！");
    }

    @Override
    public void onConnectFailed() {
        LogUtils.e("WebSocket 连接失败！");
    }

    @Override
    public void onClose() {
        LogUtils.e("WebSocket 连接关闭！");
    }

    @Override
    public void onMessage(String text) {
        LogUtils.e("WebSocket 接收消息：" + text);
    }

    /**
     * 赋值
     */
    public void changeInput(AccessibilityNodeInfo info, String text) {
        Bundle arguments = new Bundle();
        arguments.putCharSequence(AccessibilityNodeInfo.ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE,
                text);
        info.performAction(AccessibilityNodeInfo.ACTION_SET_TEXT, arguments);
    }

    /**
     * 坐标点击
     */
    public void coordinateClick(AccessibilityNodeInfo nodeInfo) {

        Rect rect = new Rect();
        nodeInfo.getBoundsInScreen(rect);
        LogUtils.e(rect.toString());
        Path path = new Path();
        path.moveTo((rect.right - rect.left) / 2, rect.bottom - ((rect.bottom - rect.top) / 2));
        GestureDescription.Builder builder;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
            builder = new GestureDescription.Builder();
            GestureDescription gestureDescription = builder
                    .addStroke(new GestureDescription.StrokeDescription(path, 0L, 10L))
                    .build();
            dispatchGesture(gestureDescription, null, null);
        }
    }


    /**
     * 返回指定操作控件
     */
    public AccessibilityNodeInfo getAccessibilityNodeInfo(String viewId, String viewText) {
        AccessibilityNodeInfo accessibilityNodeInfo = getRootInActiveWindow();
        if (accessibilityNodeInfo == null) {
            return null;
        }
        List<AccessibilityNodeInfo> gridFunctionList = accessibilityNodeInfo
                .findAccessibilityNodeInfosByViewId(viewId);
        for (AccessibilityNodeInfo nodeInfo : gridFunctionList) {
            if (viewText == null) {
                return nodeInfo;
            } else if (nodeInfo.getText().equals(viewText)) {
                return nodeInfo;
            }
        }
        return null;
    }

    public AccessibilityNodeInfo getAccessibilityNodeInfoByText(String viewText) {
        AccessibilityNodeInfo accessibilityNodeInfo = getRootInActiveWindow();
        if (accessibilityNodeInfo == null) {
            return null;
        }
        List<AccessibilityNodeInfo> gridFunctionList = accessibilityNodeInfo
                .findAccessibilityNodeInfosByText(viewText);
        for (AccessibilityNodeInfo nodeInfo : gridFunctionList) {
            if (nodeInfo.getText().equals(viewText)) {
                return nodeInfo;
            }
        }
        return null;
    }

    public AccessibilityNodeInfo getAccessibilityNodeInfo(String viewId) {
        return this.getAccessibilityNodeInfo(viewId, null);
    }

    private WindowManager windowManager;
    private WindowManager.LayoutParams layoutParams;

    private void showFloatingWindow() {
        if (Settings.canDrawOverlays(this)) {
            // 获取WindowManager服务
            windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

            // 新建悬浮窗控件
            Button button = new Button(getApplicationContext());
            button.setText("Floating Window");
            button.setBackgroundColor(Color.BLUE);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });

            // 设置LayoutParam
            layoutParams = new WindowManager.LayoutParams();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
            } else {
                layoutParams.type = WindowManager.LayoutParams.TYPE_PHONE;
            }
            layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                    | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
            layoutParams.format = PixelFormat.RGBA_8888;
            layoutParams.gravity = Gravity.END | Gravity.TOP;
            layoutParams.width = 500;
            layoutParams.height = 100;
            layoutParams.x = 0;
            layoutParams.y = 0;

            // 将悬浮窗控件添加到WindowManager
            windowManager.addView(button, layoutParams);
        }
    }


    public static class TransferAccount {
        private String name;
        private String account;
        private String opening;
        private String money;
        private String code;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getAccount() {
            return account;
        }

        public void setAccount(String account) {
            this.account = account;
        }

        public String getOpening() {
            return opening;
        }

        public void setOpening(String opening) {
            this.opening = opening;
        }

        public String getMoney() {
            return money;
        }

        public void setMoney(String money) {
            this.money = money;
        }

        public String getCode() {
            return code;
        }

        public void setCode(String code) {
            this.code = code;
        }
    }
}
